package com.propic.www;

import java.io.File;
import java.util.List;

public class Rename {

	public static void RenameOnefile(String path, String name){
		String newname = "";
		File oldfile = new File(path);
		path.endsWith(".jpg");
		if(path.endsWith(".jpg"))
			newname = name+".jpg";
		else if(path.endsWith(".jpeg"))
			newname = name+".jpeg";
		else if( path.endsWith(".gif"))
			newname = name+".gif";
		else if(path.endsWith(".png"))
			newname = name+".png";
		else if(path.endsWith(".bmp"))
			newname = name+".bmp";

        //获取父目录的地址
        String parentpath = oldfile.getParent();
        //合成新文件的绝对地址
        newname = parentpath+"/"+newname;

        File newFile = new File(newname);

        if (oldfile.getName() != newname){
            //重命名文件不存在
            if (!oldfile.exists()){
                return;
            }
            if (newFile.exists()){
                System.out.println("文件存在!");
            }else {
                oldfile.renameTo(newFile);
                System.out.println(newFile.getName()+"重命名成功");
            }
        }
    }


    public static void renameMany(List<String> renamefile, String name){
        int i = 0;
        for (String path : renamefile){
            if(i==0)
                RenameOnefile(path, name);
            else
                RenameOnefile(path, name+i);
            i++;
        }
    }





    //测试功能
	public static void main(String[] args) {
		// TODO Auto-generated method stub
        File file = new File("/home/geange/Documents/xiaomi");
        File file1 = new File("/home/geange/xiaomi2");
        file.renameTo(file1);
        System.out.println(file.getParent());

	}

}
